﻿namespace EyeOpen.SearchFile.Windows
{
	using System;
	using System.Windows.Forms;

	internal class TextBoxLogger : ILogger
	{
		private readonly TextBoxBase textBox;

		public TextBoxLogger(TextBoxBase textBox)
		{
			this.textBox = textBox;
		}

		public void Debug(string message)
		{
			WriteMessage("Debug", message);
		}

		public void Trace(string message)
		{
			WriteMessage("Searching", message);
		}

		public void Exception(Exception exception)
		{
			WriteMessage("Error", exception.Message);
		}

		private void WriteMessage(string verbosity, string message)
		{
			textBox.Invoke(
				() =>
					{
						textBox.SuspendLayout();
						textBox.Text = string.Format("{0}: {1}{2}{3}", verbosity, message, Environment.NewLine, textBox.Text);
						textBox.ResumeLayout();
					});
		}
	}
}